//
//  TKIMImageLayerUtil.m
//  TKImagePickerDemo
//
//  Created by liupm on 16/4/22.
//  Copyright © 2016年 liupm. All rights reserved.
//

#import "TKIMImageLayerUtil.h"
static TKIMImageLayerUtil *layerUtil;

@interface TKIMImageLayerUtil()
@property(nonatomic,strong) UIView *contentView;
@property(nonatomic,strong) UIView *layerView;;
@property(nonatomic,strong) UIView *tipContentView;
@property(nonatomic,strong) UILabel *tipLabel;
@property(nonatomic,strong) UIActivityIndicatorView *activityView;

@end

@implementation TKIMImageLayerUtil

+(TKIMImageLayerUtil *)shareInstance
{
    @synchronized(layerUtil)
    {
        if (!layerUtil)
        {
            layerUtil = [[TKIMImageLayerUtil alloc] init];
        }
    }
    return layerUtil;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self.tipContentView addSubview:self.tipLabel];
        [self.contentView addSubview:self.tipContentView];
    }
    return self;
}


/**
 *  @Author lpm, 16-04-22 10:04:32
 *
 *  @brief  显示等待层
 *
 *  @param tip 等待提示语
 *  @param view   容器
 */
-(void)showLoading:(NSString *)tip inView:(UIView *)view
{
    if(!view)
    {
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        view = window;
    }
    if (self.contentView.superview)
    {
        [self.contentView removeFromSuperview];
    }
    self.tipLabel.text = tip;
    self.contentView.alpha = 0;
    self.contentView.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
    self.layerView.frame =  CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
    [view addSubview:self.contentView];
    self.contentView.center = view.center;
    self.tipContentView.center = self.contentView.center;
    [UIView animateWithDuration:0.25 animations:^{
        self.contentView.alpha = 1;
    } completion:^(BOOL finished) {
    }];
}


/**
 *  @Author lpm, 16-04-22 10:04:53
 *
 *  @brief  隐藏等待层
 */
-(void)hideLoading
{
    if (self.contentView.superview)
    {
        self.contentView.alpha = 1.0;
        [UIView animateWithDuration:0.25 animations:^{
            self.contentView.alpha = 0.0;
        } completion:^(BOOL finished) {
            [self.contentView removeFromSuperview];
        }];
    }
}

#pragma mark 属性

-(UIView *)contentView
{
    if (!_contentView)
    {
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
        [_contentView addSubview:self.layerView];
    }
    return _contentView;
}

-(UIView *)layerView
{
    if (!_layerView)
    {
        _layerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
        _layerView.backgroundColor =  [UIColor colorWithRed:0.16 green:0.17 blue:0.21 alpha:1];
        _layerView.alpha = 0.5;
    }
    return _layerView;
}

-(UIView *)tipContentView
{
    if (!_tipContentView)
    {
        _tipContentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
        _tipContentView.backgroundColor =  [UIColor colorWithRed:0.16 green:0.17 blue:0.21 alpha:1];
        _tipContentView.layer.cornerRadius = 5;
        _tipContentView.layer.masksToBounds = YES;
        [_tipContentView addSubview:self.activityView];
        _tipContentView.center = self.contentView.center;
    }
    return _tipContentView;
}

-(UIActivityIndicatorView *)activityView
{
    if (!_activityView)
    {
        _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        _activityView.frame = CGRectMake(20, 10, 40, 40);
        [_activityView startAnimating];
    }
    return _activityView;
}


-(UILabel *)tipLabel
{
    if (!_tipLabel)
    {
        _tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.activityView.frame.size.height+self.activityView.frame.origin.y, self.contentView.frame.size.width, 20)];
        _tipLabel.textAlignment = NSTextAlignmentCenter;
        _tipLabel.font = [UIFont systemFontOfSize:13.0f];
        _tipLabel.textColor = [UIColor whiteColor];
        _tipLabel.backgroundColor = [UIColor clearColor];
    }
    return _tipLabel;
}


@end
